<template>
  <a-modal
    :width="1000"
    :visible="visible"
    title="评估表数据"
    :body-style="{ backgroundColor: '#F0F2F5', padding: '8px' }"
    :footer="null"
    @update:visible="updateVisible"
  >
    <div class="ele-body">
      <!-- 表格 -->
      <div>
        <a-card :bordered="false">
          <ele-pro-table
            ref="table"
            row-key="bsefId"
            :datasource="datasource"
            :columns="columns"
            :where="where"
            :scroll="{ x: 'max-content' }"
          >
            <!-- table上边工具栏 -->
            <template #toolbar>
              <a-space>
                <a-button type="primary" @click="openEdit()">
                  <template #icon>
                    <plus-outlined />
                  </template>
                  <span>新建</span>
                </a-button>
              </a-space>
            </template>

            <template #bodyCell="{ column, record }">
              <!-- table操作栏按钮 -->
              <template v-if="column.key === 'action'">
                <a-space>
                  <a @click="openEdit(record)">修改</a>
                  <a-divider type="vertical" />
                  <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                    <a class="ele-text-danger">删除</a>
                  </a-popconfirm>
                </a-space>
              </template>
            </template>
          </ele-pro-table>
        </a-card>
      </div>
      <basf-servicerecord-insects-service-evaluation-form-edit :bsrId="bsrId" :ranchId="ranchId" v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload" />
    </div>
  </a-modal>
</template>

<script>
import { PlusOutlined } from '@ant-design/icons-vue';
import { InsectsServiceEvaluationFormApi } from '@/api/basf/InsectsServiceEvaluationFormApi';
import { message } from 'ant-design-vue';
import BasfServicerecordInsectsServiceEvaluationFormEdit from './insects-service-evaluation-form-edit.vue'

export default {
  name: 'BasfServicerecordInsectsServiceEvaluationForm',
  components: {
    PlusOutlined,
    BasfServicerecordInsectsServiceEvaluationFormEdit
  },
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  created() {
    this.where.bsrId = this.data.bsrId;
    this.ranchId = this.data.ranchId;
    this.bsrId = this.data.bsrId;
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '牛舍ID',
          dataIndex: 'oxtallId',
        },
        {
          title: '牛舍',
          dataIndex: 'oxtallName',
        },
        {
          title: '虫踪迹',
          dataIndex: 'traceRecord',
          customRender: function(row) {
            if (row.text == 1) {
              return "有鼠(蝇)";
            } else if (row.text == 2) {
              return "无鼠(蝇)";
            } else {
              return "未知";
            }
          }
        },
        {
          title: '虫密度',
          dataIndex: 'densityGrade',
          customRender: function(row) {
            if (row.text == 1) {
              return "低密度";
            } else if (row.text == 2) {
              return "常规密度";
            } else if (row.text == 3) {
              return "高密度";
            } else {
              return "未知";
            }
          }
        },
        {
          title: '操作',
          key: 'action',
          width: 200,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {
        "bsrId" : this.data.bsrId
      },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      ranchId : null,
      bsrId : null
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2024/11/18 23:25
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2024/11/18 23:25
     */
    reset() {
      this.where.bsrId = this.data.bsrId;
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2024/11/18 23:25
     */
    async remove(row) {
      const result = await InsectsServiceEvaluationFormApi.delete({ bsefId: row.bsefId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2024/11/18 23:25
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /**
     * 更新编辑界面的弹框是否显示
     *
     * @param value true-显示，false-隐藏
     * @author cancan
     * @date 2022/03/02 22:49
     */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return InsectsServiceEvaluationFormApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
